package com.yiren.dbaa.module.practice_set.carmodel.view.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelHistoryContract;
import com.yiren.dbaa.module.practice_set.carmodel.presenter.CarModelHistoryPresenter;
import com.yiren.dbaa.module.practice_set.carmodel.view.adapter.CarModelVersionAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/12/6 15:19
 * @description: 车模历史版本
 */
public class CarModelHistoryActivity extends BaseActivity implements CarModelHistoryContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.lv_version_carmodel)
    ListView lvVersionCarModel;

    private CarModelEntity carModel;
    private CarModelHistoryContract.Presenter historyPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_carmodel);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        tvTitle.setText("车模回退版本");
        carModel = (CarModelEntity) getIntent().getSerializableExtra("carModel");
        historyPresenter = new CarModelHistoryPresenter(this);
        UserParamConfigBean userParamConfig = LoadConfigApi.loadUserParamConfig(this);
        if (carModel != null) {
            historyPresenter.loadVersionList(this, userParamConfig.getUserInfo().getId().toString(), carModel);
        }
    }

    @OnClick(R.id.iv_back)
    public void onClick() {
        finish();
    }

    @Override
    public void showVersionList(List<CarModelEntity> carList) {
        if (carList.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            lvVersionCarModel.setVisibility(View.GONE);
            tvTips.setText("当前暂无历史版本数据!");
        } else {
            llNoData.setVisibility(View.GONE);
            lvVersionCarModel.setVisibility(View.VISIBLE);
            CarModelVersionAdapter versionAdapter = new CarModelVersionAdapter(this, carList);
            lvVersionCarModel.setAdapter(versionAdapter);
            versionAdapter.setOnManageClickListener(position -> {
                CommonDialog.showTwoBtn(this, "操作确认", "下载会覆盖本地考规，确定下载吗？", "取消", "下载", (dialog, which) -> {
                    if (which == -2) {
                        dialog.dismiss();
                    } else {
                        carList.get(position).setName(carModel.getName());
                        carList.get(position).setCarModelVerLast(carModel.getCarModelVerLast());
                        historyPresenter.downLoadCarModelData(carList.get(position), this);
                        dialog.dismiss();
                    }
                });
            });
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
